package com.zhku.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zhku.entity.Vaccinum;
import com.zhku.entity.VaccinumType;
import com.zhku.service.IVaccinumService;
import com.zhku.service.IVaccinumTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 疫苗类型 前端控制器
 * </p>
 *
 * @author dewey
 * @since 2023-03-31
 */
@RestController
@RequestMapping(value = "/vaccinum-type")
public class VaccinumTypeController {

    //依赖业务层
    @Autowired
    IVaccinumTypeService iVaccinumTypeService;

    //使用jackson来转换map
    ObjectMapper objectMapper = new ObjectMapper();

    @RequestMapping(value = "/listall")
    public String listall() throws JsonProcessingException {
        // 定义一个map来存放json的键值对
        Map result = new HashMap();

        List<VaccinumType> list = iVaccinumTypeService.list();
        // 在结果中放一个list集合
        result.put("list", list);
        return objectMapper.writeValueAsString(result);
    }


    //查询所有的 Vaccinum 数据返回
    @RequestMapping(value = "/list")
    public String list(
            @RequestParam(required = true, defaultValue = "1") Integer pageNum,
            @RequestParam(required = true, defaultValue = "1") Integer pageSize,
            @RequestParam(required = false, defaultValue = "") String keyword  //关键词
    ) throws JsonProcessingException {
        // 定义一个map来存放json的键值对
        Map result = new HashMap();
        //分页查询
        /*分页构造函数
        Params:
        current – 当前页
        size – 每页显示条数*/

        //查询构造条件
        QueryWrapper<VaccinumType> queryWrapper = new QueryWrapper<>();
        // 成立的表达式，字段名 和数值
        queryWrapper.like(keyword != null && !keyword.equals(""), "name", keyword);

        Page<VaccinumType> page = iVaccinumTypeService.page(new Page<>(pageNum, pageSize), queryWrapper);
        // 在结果中放一个list集合
        result.put("list", page.getRecords());
        result.put("total", page.getTotal());
        // 返回json
        return objectMapper.writeValueAsString(result);
    }

    @PostMapping(value = "/insert")
    public String insertVaccinumType(VaccinumType vaccinumType) throws JsonProcessingException {
//         定义一个map来存放json的键值对
        Map result = new HashMap();

        boolean one = iVaccinumTypeService.save(vaccinumType);
        //判断数据库是否有数据
        if (one != false) {
            result.put("ok", true);
            result.put("vaccinumType", vaccinumType);
        } else {
            result.put("ok", false);
            result.put("message", "错误");
        }
        // 返回
        return objectMapper.writeValueAsString(result);
    }

    @PutMapping(value = "/update")
    public String updateVaccinumType(VaccinumType vaccinumType) throws JsonProcessingException {
//         定义一个map来存放json的键值对
        Map result = new HashMap();

        boolean one = iVaccinumTypeService.updateById(vaccinumType);
        //判断数据库是否有数据
        if (one != false) {
            result.put("ok", true);
            result.put("vaccinumType", vaccinumType);
        } else {
            result.put("ok", false);
            result.put("message", "修改疫苗种类错误");
        }
        // 返回
        return objectMapper.writeValueAsString(result);
    }

    @DeleteMapping(value = "/delete/{id}")
    public String deleteVaccinumType(@PathVariable(value = "id") Integer Id) throws JsonProcessingException {
//         定义一个map来存放json的键值对
        Map result = new HashMap();

        boolean one = iVaccinumTypeService.removeById(Id);
        //判断数据库是否有数据
        if (one != false) {
            result.put("ok", true);
            result.put("vaccinumType", Id);
        } else {
            result.put("ok", false);
            result.put("message", "删除疫苗种类错误");
        }
        // 返回
        return objectMapper.writeValueAsString(result);
    }
}
